<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(E_ALL);
class Laporan_harian extends CI_Controller {
	
        public function __construct()
	{
		parent::__construct();
                $this->load->model('laporan_harian_model','ml');
		$this->load->model('jenis_model','jp');
                if(!$this->session->userdata('logged_in'))
                {
                  redirect('login');
                }
	}
	
	function laporanHarianList($id="")
	{
		if($id != "")
		{
			$selectedLaporanHarian = $this->ml->get($id);
		}
		else
		{
			$selectedLaporanHarian = NULL;
		}
		$data['selectedLaporanHarian'] = $selectedLaporanHarian;
		$data['username'] = $this->session->userdata('logged_in');
		$data['level'] = $this->session->userdata('level');
		$data['laporan_harian'] = $this->ml->get();
		//$data['babs'] = $this->mbb->get();
		$this->load->view('laporan_harian/index', $data);
	}
	
	function addLaporanHarian($id="")
	{
		$data['username'] = $this->session->userdata('logged_in');
		$data['level'] = $this->session->userdata('level');
		$data['laporan_harian'] = $this->ml->get();
		$data['jenisPekerjaan'] = $this->jp->get();
		//$data['babs'] = $this->mbb->get();
		$this->load->view('laporan_harian/addLaporanHarian', $data);
	}
	
	function laporanHarianDetail($id)
	{
		$selectedLaporanHarian = $this->ml->get($id);
		$laporanHarianDetail = $this->ml->getDetail($id);
		$data['selectedLaporanHarian'] = $selectedLaporanHarian;
		$data['laporanHarianDetail'] = $laporanHarianDetail;
		$data['username'] = $this->session->userdata('logged_in');
		$data['level'] = $this->session->userdata('level');
		//$data['laporan_harian'] = $this->ml->get();
		//$data['babs'] = $this->mbb->get();
		$this->load->view('laporan_harian/laporanDetail', $data);
	}
	
	function printLaporanHarian($id)
	{
		$selectedLaporanHarian = $this->ml->get($id);
		$laporanHarianDetail = $this->ml->getDetail($id);
		$data['selectedLaporanHarian'] = $selectedLaporanHarian;
		$data['laporanHarianDetail'] = $laporanHarianDetail;
		$data['username'] = $this->session->userdata('logged_in');
		$data['level'] = $this->session->userdata('level');
		//$data['laporan_harian'] = $this->ml->get();
		//$data['babs'] = $this->mbb->get();
		$this->load->view('laporan_harian/print', $data);
	}
	
	function saveLaporanHarian()
	{
		$this->form_validation->set_rules('kode','Kode Pekerjaan','required');
		$this->form_validation->set_rules('cuaca','Cuaca','required');
		$this->form_validation->set_message('required','Kolom %s harus diisi dengan benar');
                
                
            if ($this->form_validation->run())
            {
				$dataLaporanHarian = array(
				'kode_pekerjaan'=> $this->input->post('kode'),
				'cuaca'=> $this->input->post('cuaca'),
				'tanggal_laporan_harian'=> date('Y-m-d'),
				'file_laporan'=> $this->input->post('userfile')
				);
				
				$config['upload_path'] = './assets/fileLaporanHarian/';
				$config['allowed_types'] = '*';
				$config['max_size'] = '100000';
				$config['file_name'] = 'laporan_harian_'.do_hash(date("Ymd").time(), 'md5');
				$this->load->library('upload', $config);
			
				if (!$this->upload->do_upload('userfile') and !empty($_FILES['userfile']['name']))
				{
					$this->session->set_flashdata('msg', '<p class="alert alert-error"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a>'.$this->upload->display_errors().'</p>');
					redirect('laporan_harian/laporanHarianList');
				}
	
				$file_data = $this->upload->data();
				$dataLaporanHarian['file_laporan'] = $config['file_name'].$file_data['file_ext'];
				
				$idLaporanDetail = $this->ml->saveLaporan($dataLaporanHarian);
				$no_pay_item = $this->input->post('no_pay_item');
				$volume = $this->input->post('volume');
				$station_awal = $this->input->post('station_awal');
				$station_akhir = $this->input->post('station_akhir');
				$tanggal_mulai = $this->input->post('tanggal_mulai');
				$jam_mulai = $this->input->post('jam_mulai');
				$tanggal_selesai = $this->input->post('tanggal_selesai');
				$jam_selesai = $this->input->post('jam_selesai');
				$count = count($no_pay_item);
				for($i=0;$i<$count;$i++)
				{
					$dataDetail = array(
					'pay_item'=> $no_pay_item[$i],
					'volume'=> $volume[$i],
					'awal'=> $station_awal[$i],
					'akhir'=> $station_akhir[$i],
					'tanggal_mulai'=> $tanggal_mulai[$i],
					'jam_mulai'=> $jam_mulai[$i],
					'tanggal_selesai'=> $tanggal_selesai[$i],
					'jam_selesai'=> $jam_selesai[$i],
					'id_laporan_harian'=> $idLaporanDetail
					);
				$hasil = $this->ml->saveLaporanDetail($dataDetail);
				}
				if($hasil == true)
				{
				$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>Laporan Harian</b> berhasil di tambah!</p>');
				redirect('laporan_harian/laporanHarianList');
				}
				else
				{
					die('error');
				}
				/*
				if($this->mb->is_boq_exist($id) == true)
				{
					//Update
					$this->mb->update($id, $boqData);
					$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$boqData['boq'].'</b> berhasil di ubah!</p>');
					redirect('boq/boqsList');
				}
				else
				{
					//Add
					$this->mb->insert($boqData);
					$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$boqData['boq'].'</b> berhasil di tambah!</p>');
					redirect('boq/boqsList');
				}
				*/
            }
            else
            {
                $this->session->set_flashdata('msg', '<p class="alert alert-error"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a>Isi form dengan benar!</p>');
				redirect('laporan_harian/addLaporanHarian');
            }        
	}
	
	function deleteLaporanHarian($pay_item="")
	{
		if($this->mb->is_boq_exist($pay_item) == true)
		{
			$this->mb->delete($pay_item);
			$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$pay_item.'</b> berhasil di hapus!</p>');
			redirect('boq/boqsList');
		}
		else
		{
			$this->session->set_flashdata('msg', '<p class="alert alert-danger"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>Tidak bisa hapus diri sendiri!</b></p>');
			redirect('boq/boqsList');
		}
	}
}